//
//  QWLocationSystem.m
//  Pods
//
//  Created by kevin on 16/1/7.
//
//

#import "QWLocationSystem.h"
#import "QWChinaLocation.h"

// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;

bool outOfChina(double lat, double lon, bool transformHKMacau);
double transformLat(double x, double y);
double transformLon(double x, double y);

// World Geodetic System ==> Mars Geodetic System
CLLocationCoordinate2D transform(double wgLat, double wgLon, bool transformHKMacau) {
    CLLocationCoordinate2D resPoint;
    double mgLat;
    double mgLon;
    if (outOfChina(wgLat, wgLon, transformHKMacau)) {
        resPoint.latitude = wgLat;
        resPoint.longitude = wgLon;
        return resPoint;
    }
    double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
    double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
    double radLat = wgLat / 180.0 * M_PI;
    double magic = sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
    dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
    mgLat = wgLat + dLat;
    mgLon = wgLon + dLon;
    
    resPoint.latitude = mgLat;
    resPoint.longitude = mgLon;
    return resPoint;
}

// transformHKMacau： 需不需要转换香港，澳门的点. 默认是不转换的
bool outOfChina(double lat, double lon, bool transformHKMacau)
{
    bool transformIt = ![[QWChinaLocation shared] isInsideChina:CLLocationCoordinate2DMake(lat, lon)];
    
    if (transformHKMacau) {
        bool inHKMacau = [[QWChinaLocation shared] isInsideHKOrMacau:CLLocationCoordinate2DMake(lat, lon)];
        
        return transformIt && !inHKMacau;
    } else {
        return transformIt;
    }
}

bool inHongKongOrMacau(double lat, double lon)
{
    return [[QWChinaLocation shared] isInsideHKOrMacau:CLLocationCoordinate2DMake(lat, lon)];
}

double transformLat(double x, double y) {
    double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));
    ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
    ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
    ret += (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
    return ret;
}

double transformLon(double x, double y) {
    double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
    ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
    ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
    ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
    return ret;
}

@implementation QWLocationSystem

+ (CLLocationCoordinate2D)getEarthFromMarsCoordinate:(CLLocationCoordinate2D)coordinate {
    if (outOfChina(coordinate.latitude, coordinate.longitude, false)) {
        return coordinate;
    }
    
    CLLocationCoordinate2D c2 = [QWLocationSystem getMarsFromEarthCoordinate:coordinate];
    return CLLocationCoordinate2DMake(2 * coordinate.latitude - c2.latitude, 2 * coordinate.longitude - c2.longitude);
}

+ (CLLocationCoordinate2D)getMarsFromEarthCoordinate:(CLLocationCoordinate2D)coordinate
{
    return [QWLocationSystem getMarsFromEarthCoordinate:coordinate transformHKMacau:NO];
}

+ (CLLocationCoordinate2D)getMarsFromEarthCoordinate:(CLLocationCoordinate2D)coor transformHKMacau:(BOOL)transformHKMacau
{
    CLLocationCoordinate2D emcp = transform(coor.latitude, coor.longitude, transformHKMacau ? true : false);
    return CLLocationCoordinate2DMake(emcp.latitude, emcp.longitude);
}

+ (CLLocationCoordinate2D)getGoogleCoordinateFromCLLocationManagerCoordinate:(CLLocationCoordinate2D)coordinate;
{
    if (inHongKongOrMacau(coordinate.latitude, coordinate.longitude)) {
        return coordinate;
    }
    
    return [QWLocationSystem getMarsFromEarthCoordinate:coordinate];
}

+ (CLLocationCoordinate2D)getAutoNaviCoordinateFromGoogleCoordinate:(CLLocationCoordinate2D)coordinate;
{
    if (inHongKongOrMacau(coordinate.latitude, coordinate.longitude)) {
        return [QWLocationSystem getMarsFromEarthCoordinate:coordinate transformHKMacau:YES];
    }
    
    return coordinate;
}

+ (CLLocationCoordinate2D)getAppleCoordinateFromGoogleCoordinate:(CLLocationCoordinate2D)coordinate;
{
    if (!outOfChina(coordinate.latitude, coordinate.longitude, false)) {
        return [QWLocationSystem getEarthFromMarsCoordinate:coordinate];
    }
    
    return coordinate;
}

@end
